<template>
    <div>
        <el-dialog :visible.sync="importDialog" top="40px" title="受案登记列表"
            :class="['avue-dialog custume-dialog' ,{'avue-dialog--top':!fullscreen},{'avue-dialog--fullscreen':fullscreen} ]"
            :fullscreen="fullscreen" width="60%" :row-save="rowSave" :before-close="handleClose">
            <avue-crud :page.sync="importPage" :option="importOption" :data="importData" :form="importForm"
                @size-change="importSizeChange" @current-change="importSCurrentChange">
                <template slot="menu" slot-scope="{type,size,...data}">
                    <el-button icon="el-icon-download" :size="size" :type="type" @click="handleImportData(data)">导入
                    </el-button>
                </template>
            </avue-crud>
            <div class="avue-dialog__footer">
                <el-button size="mini" @click="handleClose">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { add as AddArchives } from '@/api/archives/archives'
import axios from 'axios'
const SYS_YESNO = [{
    label: '否',
    value: '0'
}, {
    label: '是',
    value: '1'
}]
const XFJD_HD_CXLB = [{
    label: '简易程序',
    value: '3'
}, {
    label: '火灾登记',
    value: '1'
}, {
    label: '一般程序',
    value: '4'
}, {
    label: '非消防管辖行业',
    value: '2'
}, {
    label: '现场结案',
    value: '9'
}]
const STATUS = [{
    label: '待提交',
    value: '2'
}, {
    label: '待审批',
    value: '3'
}, {
    label: '已审批',
    value: '8'
}]
export default {
    model: {
        prop: 'importDialog',
        event: 'close'
    },
    props: {
        importDialog: {
            type: Boolean,
            default: false
        }
    },
    data() {
        return {
            fullscreen: false,
            importData: [],
            importForm: {},
            importPage: {
                currentPage: 1,
                pageSize: 10,
                total: null
            },

        }
        // SYS_YESNO,
        //     XFJD_HD_CXLB, STATUS
    },
    computed: {
        importOption() {
            return {
                border: true,
                addBtn: false,
                editBtn: false,
                delBtn: false,
                column: [
                    {
                        label: '序号',
                        prop: 'ID'
                    },
                    {
                        label: '火灾接报时间',
                        prop: 'BJSJ'
                    },
                    {
                        label: '火灾地址',
                        prop: 'HZXCDZ'
                    },
                    {
                        label: '起火场所',
                        prop: ''
                    },
                    {
                        label: '消防机构',
                        prop: 'ZGDWMC'
                    },
                    {
                        label: '虚假火灾',
                        prop: 'XJHZ',
                        dicData: SYS_YESNO
                    },
                    {
                        label: '程序类型',
                        prop: 'CXLB',
                        dicData: XFJD_HD_CXLB
                    },
                    {
                        label: '状态',
                        prop: 'STATUS',
                        dicData: STATUS
                    }
                ]
            }
        }
    },
    watch: {
        importDialog: {
            handler(val) {
                if (val) {
                    this.getImportHxData()
                    console.log('getList');
                }
            }
        }
    },
    methods: {
        handleClose() {
            this.$emit('close', false)
        },
        getImportHxData() {
            const { token_type, access_token } = this.$store.getters.hxInfo
            const $t = new Date().getTime()
            const { currentPage: current, pageSize: size } = this.importPage
            const params = {
                runId: -1, current, size, obj: {}, opid: 20024, type: 1, $t
            }
            axios({
                url: '/hxapi/api/blade-system/usual/api/pageByDwId',
                method: 'GET',
                headers: {
                    Authorization: 'Basic c2FiZXI6c2FiZXJfc2VjcmV0',
                    'Content-Type': 'application/x-www-form-urlencoded',
                    'Blade-Auth': token_type + ' ' + access_token
                },
                params
            }).then(res => {
                const { records, total } = res.data.data
                this.importData = records
                this.importPage.total = total
            })
        },
        importSizeChange(val) {
            this.importPage.currentPage = 1
            this.importPage.pageSize = 1
            this.getImportHxData()
        },
        importSCurrentChange(val) {
            this.importPage.currentPage = val
            this.getImportHxData()
        },
        handleImportData(data) {
            this.rowSave(this.importData[data.index], () => {
                this.$emit('refresh')
            }, () => { })
            this.$emit('close', false)
        },
        rowSave(row, done, loading) {
            let archivesType;
            switch (row.CXLB) {
                case '3':
                    archivesType = '0'
                    break
                case '1':
                    archivesType = '2'
                    break
                case '4':
                    archivesType = '1'
                    break
            }
            let data = {
                name: row.HZQHDW,
                state: row.STATUS,
                archivesType: archivesType,
                address: row.HZXCDZ,
                fireName: '',
                callTime: row.BJSJ,
                doOrg: row.ZGDWMC,
            }
            // return
            const callback = () => {
                delete this.importForm.params
                AddArchives(data)
                    .then(data => {
                        if (data.code === 200) {
                            this.getImportHxData()
                            this.$message.success('新增成功')
                            done()
                        } else {
                            this.$message.error(data.msg)
                            loading()
                        }
                    })
                    .catch(() => {
                        loading()
                    })
            }
            callback()
        }
    }
}
</script>
